package com.quanquan.web;

import com.quanquan.common.Response;
import com.quanquan.entity.User;
import com.quanquan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginContrller {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Response login (String userName, String password) {
        if (userName == null || password == null) {
            return new Response().failure("用户名或密码不能为空！");
        }
        User user = userService.findUserByUserName(userName);
        if (user == null) {
            return new Response().failure("该用户不存在");
        }
        if (!password.equals(user.getPassword())) {
            return new Response().failure("用户名和密码不匹配！");
        }
        user.setPassword("*******");
        return new Response().success(user);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Response register (User param) {
        User user = new User();
        if (param.getUserName() != null) user.setUserName(param.getUserName());
        if (param.getPassword() != null) user.setPassword(param.getPassword());
        userService.save(user);
        return new Response().success("注册成功");
    }

//    public static void main(String[] args) {
////        int length = 10;
////        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
////        Random random = new Random();
////        StringBuffer sb = new StringBuffer();
////        for (int i = 0; i < length; i++) {
////            int number = random.nextInt(base.length());
////            sb.append(base.charAt(number));
////        }
////        System.out.println(sb);
//
////        BigDecimal b=new BigDecimal("1");
////        BigDecimal c=new BigDecimal("2");
////        System.out.println(b.subtract(c));
//
////        String str = "0900";
////            Pattern pattern = Pattern.compile("[0-9]*");
////
////            Boolean a =  pattern.matcher(str).matches();
////        System.out.println(a);
//        String sts = "[123,1234]";
//        System.out.println(JsonUtil.toList(Long.class, sts));
//        List<Long> kPointDifficultyIds = new ArrayList<>();
////        kPointDifficultyIds.add(1L);
//        kPointDifficultyIds.add(2L);
//        List<Long> kPointDifficultyIdList = new ArrayList<>();
//        kPointDifficultyIdList.add(1L);
//        kPointDifficultyIdList.add(2L);
//        List<Long> addIds = ListUtils.subtract(kPointDifficultyIds, kPointDifficultyIdList);
//        List<Long> deleteIds = ListUtils.subtract(kPointDifficultyIdList, kPointDifficultyIds);
//        System.out.println("addIds is " + addIds);
//        System.out.println("deleteIds is " + deleteIds);
//
//    }
//    }
//    }
}
